Fix connection closure handling in Protocol and InMemoryTransport#833
Open
alasano wants to merge 2 commits intomodelcontextprotocol:mainfrom
Open
Fix connection closure handling in Protocol and InMemoryTransport#833alasano wants to merge 2 commits intomodelcontextprotocol:mainfrom
alasano wants to merge 2 commits intomodelcontextprotocol:mainfrom
Conversation
- Make close() idempotent with _isClosed flag - Prevent infinite recursion by clearing _otherTransport before calling peer.close() - Ensure both transports' onclose callbacks fire - Add tests for idempotency, concurrent closes, and post-close behavior Fixes deadlock where closing one transport would not properly close its peer, causing promises to hang indefinitely.
- Track request handlers with AbortControllers in _requestHandlerAbortControllers - Abort all active request handlers in _onclose() to prevent hanging operations - Ensure both outgoing requests (response handlers) and incoming requests (request handlers) are properly terminated Fixes issue where request handlers would continue running after connection closure, causing timeouts and resource leaks.
Author
|
Hey @ihrpr , this PR fixes critical bugs causing indefinite connection hangs in multi-user scenarios. The fixes are also non-breaking with proper test coverage. Could you please review when you have time? Or assign someone else if possible, we've been running our own version with the fix but need it in the official package ideally. |
ihrpr
requested changes
Aug 18, 2025
Contributor
ihrpr
left a comment
There was a problem hiding this comment.
Thank you so much for the fixes!
Please can tests be added for the Protocol abort functionality as well?
Author
Thank you for getting back to me as well 🙏 Absolutely, I'll add some a bit later today |
0845a57 to
c94ba4b
Compare
|
Would #611 be fixed by this PR? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fix connection closure handling in Protocol and InMemoryTransport
Motivation and Context
This PR fixes two critical bugs that cause promises to hang indefinitely when connections are closed:
onclosecallback was never fired, causing server-side promises to hang foreverThese bugs manifested as timeout errors in applications where connections were closed while requests were in-flight (e.g., user disconnection during a pending UI prompt).
How Has This Been Tested?
elicitation/createrequestelicitation/create) would cause tests to hang indefinitelyBreaking Changes
None. These are bug fixes that make the SDK behave as expected.
Types of changes
Checklist
Additional context
The fix ensures proper cleanup in both directions:
onclosecallbacks